package com.yellowzp.demoapisql.apisql;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice;

import java.io.IOException;
import java.lang.reflect.Type;

@RestControllerAdvice
@Slf4j
public class SqlStatRequestBodyAdvice implements RequestBodyAdvice {

    @Autowired
    private SqlEncryptor sqlEncryptor;

    @Override
    public boolean supports(MethodParameter methodParameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
//        log.info("MyRequestBodyAdvice supports triggered for method: {}", methodParameter.getMethod().getName());
        return true;
    }

    @Override
    public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException {
//        log.info("MyRequestBodyAdvice beforeBodyRead triggered for parameter: {}", parameter.getParameterName());
        return inputMessage;
    }

    @Override
    public Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
//        log.info("MyRequestBodyAdvice afterBodyRead triggered with body: {}", body);

        // 获取目标类的Class对象
        Class<?> targetClass = parameter.getParameterType();
        if (targetClass != null) {
            // 遍历目标类的所有字段
            for (java.lang.reflect.Field field : targetClass.getDeclaredFields()) {
                // 检查字段是否标注了@SqlStat注解
                if (field.isAnnotationPresent(SqlStat.class) && field.getType() == String.class) {
                    field.setAccessible(true); // 确保可以访问私有字段
                    try {
                        Object fieldValue = field.get(body); // 获取字段值
                        if (fieldValue != null) {
                            // 对字段值进行解密
                            fieldValue = sqlEncryptor.decrypt((String) fieldValue);
                            // 设置解密后的值
                            field.set(body, fieldValue);
                        }
//                        log.info("Found field with @SqlStat: {} = {}", field.getName(), fieldValue);
                    } catch (IllegalAccessException e) {
                        log.error("Error accessing field: {}", field.getName(), e);
                    } catch (Exception e) {
                        log.error(e.getMessage(), e);
                    }
                }
            }
        }

        return body;
    }

    @Override
    public Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {
//        log.info("MyRequestBodyAdvice handleEmptyBody triggered for parameter: {}", parameter.getParameterName());
        return body;
    }
}